let mysql      = require('mysql');
let bdConfig      = require('../config/database').offline;

let order =  {
	create: function (f, callback) {
		let connection = mysql.createConnection(bdConfig);
		connection.connect();
		let sql = 'INSERT INTO `t_order` ( `p_title`, `o_price`, `p_id`, `o_copies`, `p_cover`,	`create_date`) VALUES  ( "' + f.p_title + '", "' + f.o_price + '", "' + f.p_id + '", "' + f.o_copies + '", "' + f.p_cover + '","'+f.create_date+'");  ';
		connection.query(sql,function (err, result) {
			if(err){
				callback(err);
				return;
			}
			callback(result);
		});
		connection.end();
	},
	list:function (f,callback) {
		let connection = mysql.createConnection(bdConfig);
		connection.connect();
		let sql = 'SELECT * from  `t_order` Order By `id` DESC' ;
		connection.query(sql,function (err, result) {
			if(err){
				callback(err);
				return;
			}
			callback(result);
		});
		connection.end();
	},
	detail:function (id,callback) {
		let connection = mysql.createConnection(bdConfig);
		connection.connect();
		let sql = 'SELECT * from  `t_order` where id = ' + id;
		connection.query(sql,function (err, result) {
			if(err){
				callback(err);
				return;
			}
			callback(result);
		});
		connection.end();
	},
	total:function (callback) {
		let connection = mysql.createConnection(bdConfig);
		connection.connect();
		let sql = 'SELECT COUNT(*) AS `total` FROM `t_order`  ';
		connection.query(sql,function (err, result) {
			if(err){
				callback(err);
				return;
			}
			callback(result);
		});
		connection.end();
	}



};

module.exports = order;


